/*
 * 11文件读写结构体.c
 *
 *  Created on: 2024年3月17日
 *      Author: HP
 */
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>

typedef struct student {
	char name[21];
	int age;
	int score;
	char addr[51];
}stu;

int main1011(){
	stu ss[3] = {
		{"黄某", 9, 10, "河北"},
		{"马某", 3, 1000, "邯郸"},
		{"大法师", 30, 1, "黑龙江"}
	};
	FILE* fp = fopen("D:/zsl/d.txt", "wb");
	for(int i=0; i<3; i++){
		fwrite(&ss[i], sizeof(stu), 1, fp);
	}
	fclose(fp);

	FILE* fp2 = fopen("D:/zsl/d.txt", "rb");
	stu* ss2 = (stu*)malloc(sizeof(stu)*3);
	int i = 0;
	while(!feof(fp2)){
		fread(&ss2[i], sizeof(stu), 1, fp2);
		i++;
	}
	for(int i=0; i<3; i++){
		printf("姓名：%s\n", ss2[i].name);
		printf("年龄：%d\n", ss2[i].age);
		printf("成绩：%d\n", ss2[i].score);
		printf("地址：%s\n", ss2[i].addr);
	}
	fclose(fp2);
}
